<?php
/**
 * Created by PhpStorm.
 * User: Huyang
 * Date: 2016/9/28
 * Time: 14:56
 */

namespace App\Http\Repositories\Daos\Impls;


use App\Http\Repositories\Daos\IUserDao;
use App\Http\Repositories\Eloquent\User;

class UserDao extends BaseDao implements IUserDao
{
    public function __construct(User $model)
    {
        parent::__construct($model);
    }

    public function byMobile($mobile, $excludeId = 0) {

        $filterMap = $this->model->newQuery();
        if ($excludeId) {
            $filterMap->where('id','!=', $excludeId);
        }
        return $filterMap->where('mobile', $mobile)->first();

    }

    public function lists($search,$page, $size)
    {
        $filterMap = $this->model->newQuery();

        if (isset($search['user_id']) && !empty($search['user_id'])) {
            $filterMap->where('id', $search['user_id']);
        }

        if (isset($search['mobile']) && !empty($search['mobile'])) {
            $filterMap->where('mobile', $search['mobile']);
        }

        if (isset($search['name']) && !empty($search['name'])) {
            $filterMap->where('name','like', '%'.$search['name'].'%');
        }

        if (isset($search['username']) && !empty($search['username'])) {
            $filterMap->where('username','like', '%'.$search['username'].'%');
        }

        if (isset($search['is_job']) && $search['is_job'] != null) {
            $filterMap->where('is_job', $search['is_job']);
        }

        $order = ['created_at'=>'desc'];
        if (isset($search['order']) && is_array($search['order'])) {
            $order = $search['order'];
        }
        return $this->page($filterMap,$page, $size, $order);
    }

    public function byName($name)
    {
        $filterMap = $this->model->newQuery();
        $filterMap->where('username', $name);
        return $filterMap->first();
    }

}